package model;
import java.util.ArrayList;
public class ProductContainer
{
 private ArrayList<Product> ProductCollection;
 private static ProductContainer instance;

  public ProductContainer()
 {
   ProductCollection=new ArrayList<Product>();

 }
 public static ProductContainer getInstance()
{
 if (instance==null) 
    {
     instance= new ProductContainer();
    }
    return instance;
}
 //+addProduct(): void
 public void addProduct(Product product)
 {
  ProductCollection.add(product);
 }
 

 //+printProduct(): void
 public void printProduct()
 {
     System.out.println("List of Products:");
     for(Product product:ProductCollection)
     {System.out.println(""+ product.getBarcode());
      System.out.println("");
      System.out.println(""+ product.getName());}
     
 }
 //+removeProduct(): void
 /*public void removeProduct(Product Product)
 {
    ProductCollection.remove(Product);
    
    
 }*/
 //+updateProduct(): boolean
/* public boolean updateProduct(String barcode,String name,String)
{
 
}
*/
 
 
 public void removeProduct(String barcode)
 {
 Product product=findProduct(barcode); 
        if(product !=null){
            boolean ok=ProductCollection.remove(product);
            if(ok){
                System.out.println("Product with the barcode " + barcode + " is now deleted");
                  } 
                }
            else{ System.out.println("There is no product with the barcode " + barcode);
                }
           }
        
//+findProduct(barcode)

 
public Product findProduct(String barcode) {
    Product product=null;
    int index=0;
    boolean found=false;
    while(index < ProductCollection.size() && !found) {
        product=ProductCollection.get(index);
        if(product.getBarcode().equals(barcode)) {
            found=true;
        } else {
            index++;
        }
    }
    if(found) {
        return ProductCollection.get(index);
    } else {
        return null;
    }
}  

 } 
